package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.TMStatBakMapper;
import cc.rengu.igas.momp.common.entity.TMStatBak;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * @author liujinan
 * @date 2021/7/9 17:21
 * @description 统计信息备份表数据库操作接口实现
 */
public class TMStatBakMapperImpl implements TMStatBakMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MOMP_POOL_NAME);

    /**
     * 统计信息备份表数据库操作定义
     *
     * @param instId         法人机构号
     * @param statType       统计类型
     * @param statPrimaryKey 统计表对应主键信息
     * @return cc.rengu.igas.momp.common.entity.TMStatBak 统计信息备份表
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2021/7/9 12:29
     */
    @Override
    public TMStatBak selectTMStatBakByStatPrimaryKey(String instId, String statType, String statPrimaryKey) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MOMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectTMStatBakByStatPrimaryKey");

        // 按接口赋查询条件
        TMStatBak tmStatBak = new TMStatBak();
        tmStatBak.setInstId(instId);
        tmStatBak.setStatType(statType);
        tmStatBak.setStatPrimaryKey(statPrimaryKey);

        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, tmStatBak, tmStatBak);
        if (0 != iReturnCode) {
            rglog.error("根据查询条件法人机构号<{}> 统计表对应主键信息<{}>统计信息备份表失败!<{}>", instId, statPrimaryKey, iReturnCode);
            return null;
        }

        return tmStatBak;
    }

}
